package com.example.speeddating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.entity.Bullet;
import com.example.speeddating.mapper.BulletMapper;
import com.example.speeddating.service.BulletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author tina
 * @since 2023-08-07 12:03:44
 */
@Service
public class BulletServiceImpl extends ServiceImpl<BulletMapper, Bullet> implements BulletService {

    @Autowired
    private BulletMapper bulletMapper;

    @Override
    public Page<Bullet> recentBullets(Integer pageNum, Integer pageSize) {
        Page<Bullet> bulletPage = new Page<>(pageNum, pageSize);
        QueryWrapper<Bullet> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return bulletMapper.selectPage(bulletPage,queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Bullet wish(Long id, String content) {
        Bullet bullet = new Bullet();
        bullet.setUserId(id);
        bullet.setContent(content);
        bulletMapper.insert(bullet);
        return bullet;
    }
}
